{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import cv2\n",
    "import pickle\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.svm import SVC\n",
    "dir = \"data/Training\"\n",
    "categories = ['Apple Braeburn','Grape Blue','Pineapple','Strawberry','Banana','Orange','Pomegranate']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "for category in categories:\n",
    "    path = os.path.join(dir,category)\n",
    "    label = categories.index(category)\n",
    "    \n",
    "    for img in os.listdir(path):\n",
    "        imgpath = os.path.join(path,img)\n",
    "        fruit_img=cv2.imread(imgpath,0)\n",
    "        #cv2.imshow('image',fruit_img)\n",
    "        try:\n",
    "            fruit_img =cv2.resize(fruit_img,(50,50))\n",
    "            image = np.array(fruit_img).flatten()\n",
    "\n",
    "            data.append([image,label])\n",
    "        except Exception as e:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dir = \"data/Test\"\n",
    "categories = ['Apple Braeburn','Grape Blue','Pineapple','Strawberry','Banana','Orange','Pomegranate']\n",
    "data = []\n",
    "for category in categories:\n",
    "    path = os.path.join(dir,category)\n",
    "    label = categories.index(category)\n",
    "    \n",
    "    for img in os.listdir(path):\n",
    "        imgpath = os.path.join(path,img)\n",
    "        fruit_img=cv2.imread(imgpath,0)\n",
    "        #cv2.imshow('image',fruit_img)\n",
    "        try:\n",
    "            fruit_img =cv2.resize(fruit_img,(50,50))\n",
    "            image = np.array(fruit_img).flatten()\n",
    "\n",
    "            data.append([image,label])\n",
    "        except Exception as e:\n",
    "            pass\n",
    "pick_in = open(\"data_test.pickle\",'wb')\n",
    "pickle.dump(data,pick_in)\n",
    "pick_in.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pick_in = open(\"data1.pickle\",'wb')\n",
    "pickle.dump(data,pick_in)\n",
    "pick_in.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pick_in = open('data1.pickle','rb')\n",
    "data = pickle.load(pick_in)\n",
    "pick_in.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(data)\n",
    "features = []\n",
    "labels = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for feature,label in data:\n",
    "    features.append(feature)\n",
    "    labels.append(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xtrain,xtest,ytrain,ytest = train_test_split(features,labels,test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = SVC(C=1,kernel='poly',gamma='auto')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.fit(xtrain,ytrain)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accurancy = model.score(xtest,ytest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "accurancy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pick = open('model.sav','wb')\n",
    "pickle.dump(model,pick)\n",
    "pick.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits=[]\n",
    "prediction = model.predict(xtest)\n",
    "for i in range(len(prediction)):\n",
    "    fruits.append(categories[prediction[i]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install PCV\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
